package com.yanqu.road.server.cmd.activity.douluodalu;

import com.yanqu.road.entity.activity.douluodalu.config.DouLuoDaLuConfig;
import com.yanqu.road.entity.activity.douluodalu.data.DouLuoDaLuPatronsTimesData;
import com.yanqu.road.entity.activity.douluodalu.data.DouLuoDaLuServerUserData;
import com.yanqu.road.pb.activity.douluodalu.DouLuoDaLuProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.douluodalu.DouLuoDaLuModule;
import com.yanqu.road.server.manger.activity.douluodalu.DouLuoDaLuMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_DOU_LUO_DA_LU_DISPATCH_PATRONS,desc = "派遣出战门客")
public class DouLuoDaLuDispatchPatronsCmd extends AbstractCommand {


    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if ((ret = DouLuoDaLuMgr.canEntryActivity(player)) != 0) return;

        if ((ret = DouLuoDaLuMgr.activityInDuiJuePeriod()) != 0) return;

        DouLuoDaLuConfig config = DouLuoDaLuMgr.getConfig();

        DouLuoDaLuProto.DouLuoDaLuDispatchPatronsReqMsg reqMsg = DouLuoDaLuProto.DouLuoDaLuDispatchPatronsReqMsg.parseFrom(packet.getBytes());
        int type = 1;
        int patronsId = reqMsg.getPatronsId();
        if(type != 1 && type !=2){
            ret = GameErrorCode.E_REQUEST_ARGUMENT_ERROR;
            return;
        }
        DouLuoDaLuModule daLuModule = player.getModule(DouLuoDaLuModule.class);
        DouLuoDaLuPatronsTimesData patronsTimesData = daLuModule.getPatronsTimesDataMap().get(patronsId);
        DouLuoDaLuServerUserData userData = daLuModule.getUserData();
        if(type == 1){
            //门客次数判断
            if(userData.getSendTimes() >= config.FIGHT_GUEST_FREETIMES){
                ret = GameErrorCode.E_DOU_LUO_DA_LU_NO_GUEST_FREE_TIMES;
                return;
            }
            if(patronsTimesData != null && patronsTimesData.getSendTimes() >= config.FIGHT_GUEST_PAIQIAN){
                ret = GameErrorCode.E_DOU_LUO_DA_LU_NO_PATRONS_PAIQIAN_TIMES;
                return;
            }
        }else {
            if(patronsTimesData != null && patronsTimesData.getFightTimes() >= config.FIGHT_GUEST_OUT){
                ret = GameErrorCode.E_DOU_LUO_DA_LU_NO_PATRONS_OUT_TIMES;
                return;
            }

        }

        //预扣次数
        userData.setSendTimes(1 + userData.getSendTimes());
        if(patronsTimesData == null){
            daLuModule.initPatronsTimesData(patronsId, 1, type);
        }else {
            if(type == 1){
                patronsTimesData.setSendTimes(1 + patronsTimesData.getSendTimes());
            }else {
                patronsTimesData.setFightTimes(1 + patronsTimesData.getFightTimes());
            }
        }

        DouLuoDaLuProto.DouLuoDaLuDispatchPatronsCrossReqMsg.Builder crossReq = DouLuoDaLuProto.DouLuoDaLuDispatchPatronsCrossReqMsg.newBuilder();
        crossReq.setActivityId(DouLuoDaLuMgr.getActivityInfo().getActivityId());
        crossReq.setPatronsId(reqMsg.getPatronsId());
        crossReq.setBattleType(type);
        player.sendUnionActivityGroupPacket(Protocol.C_DOU_LUO_DA_LU_DISPATCH_PATRONS, crossReq, crossReq.getActivityId());

    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            player.sendPacket(Protocol.U_DOU_LUO_DA_LU_DISPATCH_PATRONS, DouLuoDaLuProto.DouLuoDaLuDispatchPatronsRespMsg.newBuilder().setRet(ret));
        }
    }
}
